<?php

namespace App\Http\Controllers\CRM;

use App\Enums\OrderFrom;
use App\Enums\OrderItemType;
use App\Enums\OrderType;
use App\Enums\TradeStatus;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Models\Member;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Store;
use Illuminate\Http\Request;

class OverviewController extends Controller
{
    public function member(Store $store)
    {
        // 充值余额
        $totalRecharge = Member::sum('money');

        // 赠送余额
        $totalGiveMoney = Member::sum('give_money') + Member::sum('table_give_money');

        // 总余额
        $totalBalance = $totalRecharge + $totalGiveMoney;

        return ApiResponseHelper::json([
            'total_recharge' => (float)$totalRecharge,
            'total_give_money' => (float)$totalGiveMoney,
            'total_balance' => (float)$totalBalance,
            //卡片
            'card_total_recharge' => 0,
            'card_total_give_money' => 0,
            'card_total_balance' => 0,
        ]);
    }

    // 营业额
    public function turnover(Store $store)
    {
        $orderIds = Order::where([
            'status' => TradeStatus::Payed,
            'store_id' => $store->id,
            'type' => OrderType::Counter,
            'from' => OrderFrom::Counter,
        ])->pluck('id');

        // 总营业额
        $totalTurnover = Order::whereIn('id', $orderIds)->sum('money');

        // 线上营业额
        $onlineTurnover = $totalTurnover;

        // 线下营业额
        $offlineTurnover = 0;

        // 商品销售额
        $productTurnover = OrderItem::whereIn('order_id', $orderIds)->where('type', OrderItemType::Goods)->sum('money');

        // 超级会员总额
        $superMemberTurnover = 0;

        // 快捷支付总额
        $quickPayTurnover = 0;

        // 台桌费
        $tableTurnover = OrderItem::whereIn('order_id', $orderIds)->where('type', OrderItemType::Table)->sum('money');

        // 助教费
        $assistantTurnover = OrderItem::whereIn('order_id', $orderIds)->where('type', OrderItemType::Assistant)->sum('money');

        // 助教费构成
        $assistantTurnoverDetail = [
            // 排课费
            'course' => 0,
            // 点课费
            'point' => (float)$assistantTurnover,
            // 选课费
            'select' => 0,
            // 加课费
            'add' => 0,
            // 买课费
            'buy' => 0,
        ];

        return ApiResponseHelper::json([
            'total_turnover' => (float)$totalTurnover,
            'online_turnover' => (float)$onlineTurnover,
            'offline_turnover' => (float)$offlineTurnover,
            'product_turnover' => (float)$productTurnover,
            'super_member_turnover' => (float)$superMemberTurnover,
            'quick_pay_turnover' => (float)$quickPayTurnover,
            'table_turnover' => (float)$tableTurnover,
            'assistant_turnover' => (float)$assistantTurnover,
            'assistant_turnover_detail' => $assistantTurnoverDetail,
        ]);
    }

    // 利润数据说明
    public function profit(Store $store)
    {
        // 利润
        $profit = 0;

        // 营业额
        $turnover = 0;

        // 商品销售成本
        $productCost = 0;

        // 费用
        $cost = 0;

        // 挂账结款的钱
        $badMoney = 0;

        // 助教佣金
        $assistantCommission = 0;

        // 营业额中会员支付赠送部分的钱
        $memberGiveMoney = 0;

        // 营业额中卡片支付赠送部分的钱
        $cardGiveMoney = 0;

        // 营业额中优惠券支付的钱
        $couponMoney = 0;

        // 营业额中挂账支付的钱
        $badPayMoney = 0;

        // 记账本金额
        $bookMoney = 0;

        // 记账本支出金额
        $bookOutMoney = 0;

        // 记账本收入金额
        $bookInMoney = 0;

        return ApiResponseHelper::json([
            'profit' => (float)$profit,
            'turnover' => (float)$turnover,
            'product_cost' => (float)$productCost,
            'cost' => (float)$cost,
            'bad_money' => (float)$badMoney,
            'assistant_commission' => (float)$assistantCommission,
            'member_give_money' => (float)$memberGiveMoney,
            'card_give_money' => (float)$cardGiveMoney,
            'coupon_money' => (float)$couponMoney,
            'bad_pay_money' => (float)$badPayMoney,
            'book_money' => (float)$bookMoney,
            'book_out_money' => (float)$bookOutMoney,
            'book_in_money' => (float)$bookInMoney,
        ]);
    }

    public function assistant(Store $store)
    {
        // 总时长
        $totalDetail = [
            // 原始时长
            'original' => 0,
            // 营收时长
            'revenue' => 0,
            // 真实时长
            'real' => 0,
            // 支付时长
            'pay' => 0,
            // 优惠时长
            'discount' => 0,
            // 退款时长
            'refund' => 0,
        ];

        // 上课时长
        $courseDetail = [
            // 原始时长
            'original' => 0,
            // 营收时长
            'revenue' => 0,
            // 真实时长
            'real' => 0,
            // 支付时长
            'pay' => 0,
            // 优惠时长
            'discount' => 0,
            // 退款时长
            'refund' => 0,
        ];

        // 买课时长
        $buyDetail = [
            // 原始时长
            'original' => 0,
            // 营收时长
            'revenue' => 0,
            // 真实时长
            'real' => 0,
            // 支付时长
            'pay' => 0,
            // 优惠时长
            'discount' => 0,
            // 退款时长
            'refund' => 0,
        ];

        // 总金额
        $totalMoneyDetail = [
            // 原始金额
            'original' => 0,
            // 营收金额
            'revenue' => 0,
            // 佣金金额
            'commission' => 0,
            // 支付金额
            'pay' => 0,
            // 优惠金额
            'discount' => 0,
            // 退款金额
            'refund' => 0,
        ];

        // 上课金额
        $courseMoneyDetail = [
            // 原始金额
            'original' => 0,
            // 营收金额
            'revenue' => 0,
            // 佣金金额
            'commission' => 0,
            // 支付金额
            'pay' => 0,
            // 优惠金额
            'discount' => 0,
            // 退款金额
            'refund' => 0,
        ];

        // 买课金额
        $buyMoneyDetail = [
            // 原始金额
            'original' => 0,
            // 营收金额
            'revenue' => 0,
            // 佣金金额
            'commission' => 0,
            // 支付金额
            'pay' => 0,
            // 优惠金额
            'discount' => 0,
            // 退款金额
            'refund' => 0,
        ];

        return ApiResponseHelper::json([
            'total_detail' => $totalDetail,
            'course_detail' => $courseDetail,
            'buy_detail' => $buyDetail,
            'total_money_detail' => $totalMoneyDetail,
            'course_money_detail' => $courseMoneyDetail,
            'buy_money_detail' => $buyMoneyDetail,
        ]);
    }

    public function table(Store $store)
    {
        // 消费会员数
        $memberCount = 0;

        // 客单数
        $orderCount = 0;

        // 金额
        $moneyDetail = [
            // 原始金额
            'original' => 0,
            // 优惠金额
            'discount' => 0,
            // 支付金额
            'pay' => 0,
            // 退款金额
            'refund' => 0,
            // 实际金额
            'real' => 0,
        ];

        // 时长
        $timeDetail = [
            // 原始时长
            'original' => 0,
            // 优惠时长
            'discount' => 0,
            // 支付时长
            'pay' => 0,
            // 退款时长
            'refund' => 0,
            // 实际时长
            'real' => 0,
        ];

        return ApiResponseHelper::json([
            'member_count' => $memberCount,
            'order_count' => $orderCount,
            'money_detail' => $moneyDetail,
            'time_detail' => $timeDetail,
        ]);
    }
}
